package innatour;

import java.util.Iterator;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class toRoomDetails extends innatour.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();
 

  
	 @Test
	  public void tOdetails() throws Exception {
		//driver.get("https://inna.ru/#/packages/search/6733-6623-10.04.2015-11.04.2015-0-1-?hotel=435490&ticket=1734707199");	
		for (int second = 0;; second++) {
			  if (second >= 300) fail("timeout");
		  	try { if (isElementPresent(By.cssSelector(".b-list-panel"))) break; } catch (Exception e) {}
		  	Thread.sleep(1000);
		  }
		  
		  
		driver.findElement(By.linkText("Купить")).click();
		Set<String> windows = driver.getWindowHandles();
		 
		 Iterator<String> it = windows.iterator();
		  String mainWindow = (String) it.next();
		  String popUp = (String) it.next();
		  driver.switchTo().window(popUp);
		  //driver.close();
		  //driver.switchTo().window(mainWindow);
			
	  
	  
	 
		  for (int second = 0;; second++) {
		    	if (second >= 300) fail("timeout");
		    	try { if (isElementPresent(By.xpath("//*[@class='hotel-details-rooms hotel-details-room_show_NONE']"))) break; } catch (Exception e) {}
		    	Thread.sleep(1000);
		    }
		 int numberscount = driver.findElements(By.xpath("//span[contains(.,'Купить')]")).size();
		  
		 WebElement button=null;
		   
		  for(int i=1;i<numberscount;i++)
		  {
			  WebElement hotelBlock = driver.findElement(By.xpath("(//div[@class='hotel-details-rooms__room-desc_wrap'])["+i+"]"));			  		  
			  hotelBlock.click();
			  WebElement hotelBlockParent = driver.findElement(By.xpath("(//div[@class='hotel-details-rooms__room ng-scope hotel-details-rooms__room_state_open'])["+i+"]"));
			  button = hotelBlockParent.findElement(By.cssSelector(".btn.btn-large.btn-green.btn-hotel-room-actions.ng-scope"));
			  if(hotelBlockParent.findElement(By.cssSelector("div.ng-scope > p.ng-binding")).getText().contains(
					  "приведут к уплате неустойки в сумме стоимости номера за сутки и налога, умноженной на 1. Данный отель не выплачивает компенсацию в случае неявки или преждевременного отъезда"))
			  {
				  break;  
			  }		  
		  }  
	  	
	  
		  if (button!=null)
		  {
			  button.click();
			  //int h=0;
			  //h++;
		  }

	  }
	  
	 private boolean isElementPresent(By by) {
		    try {
		      driver.findElement(by);
		      return true;
		    } catch (NoSuchElementException e) {
		      return false;
		    }
		  }

		  private String closeAlertAndGetItsText() {
		    try {
		      Alert alert = driver.switchTo().alert();
		      String alertText = alert.getText();
		      if (acceptNextAlert) {
		        alert.accept();
		      } else {
		        alert.dismiss();
		      }
		      return alertText;
		    } finally {
		      acceptNextAlert = true;
		    }
		  }
		}

